/**
 @Name：大学生计算机课程平时成绩管理系统 成绩管理
 @Author：dingwen
 */

layui.define(['table', 'form'], function (exports) {
    let $ = layui.$
        , table = layui.table
        , setter = layui.setter

    let total = 0;

    $(function () {
        /*获取总记录条数*/
        $.ajax({
            url: layui.setter.baseUrl + '/score/total',
            type: 'get',
            dateType: "json",
            contentType: 'application/json',
            success: function (res) {
                if (res.code === 200) {
                    total = res.data;
                    // 表格数据渲染
                    table.render({
                        elem: '#LAY-score-manage'
                        , page: true
                        , url: layui.setter.baseUrl + '/score/info/page'
                        , toolbar: '#toolbarDemo'
                        , title: '成绩表'
                        , parseData: function (res) {
                            return {
                                "code": 0,
                                "msg": res.message,
                                "count": total,
                                "data": res.data
                            };
                        }
                        , request: {
                            pageName: 'pageNum'
                            , limitName: 'pageSize'
                        }
                        , cols:
                            [[
                                {field: 'scoreId', width: 200, title: 'ID', sort: true}
                                , {field: 'studentId', width: 200, title: '学号',}
                                , {field: 'studentName', width: 100, title: '姓名',}
                                , {field: 'courseId', width: 200, title: '课程ID'}
                                , {field: 'courseName', width: 200, title: '课程名称'}
                                , {field: 'courseType', width: 200, title: '类型', templet: '#course-type'}
                                , {field: 'clazzId', width: 200, title: '班级编号'}
                                , {field: 'clazzName', title: '班级名称', width: 200}
                                , {
                                    field: 'scoreAttendance',
                                    title: '考勤成绩',
                                    minWidth: 100,
                                    style: 'background-color: #3498db; color: #fff;',
                                    edit: 'text',
                                    sort: true
                                }
                                , {
                                    field: 'scoreHomework',
                                    title: '作业成绩',
                                    minWidth: 100,
                                    style: 'background-color: #3498db; color: #fff;',
                                    edit: 'text',
                                    sort: true
                                }
                                , {
                                    field: 'scoreClazz',
                                    title: '上课成绩',
                                    minWidth: 100,
                                    style: 'background-color: #3498db; color: #fff;',
                                    edit: 'text',
                                    sort: true
                                }
                                , {
                                    field: 'scoreMiddle',
                                    title: '期中成绩',
                                    minWidth: 100,
                                    style: 'background-color: #3498db; color: #fff;',
                                    edit: 'text',
                                    sort: true
                                }
                                , {
                                    field: 'scoreEnd',
                                    title: '期末成绩',
                                    minWidth: 100,
                                    style: 'background-color: #3498db; color: #fff;',
                                    edit: 'text',
                                    sort: true
                                }
                                , {
                                    field: 'scoreFinal',
                                    title: '最终成绩',
                                    minWidth: 100,
                                    style: 'background-color: #3498db; color: #fff;',
                                    edit: 'text',
                                    sort: true
                                }
                            ]]
                    });
                } else {
                    layer.msg("失败");
                }
            }
        });
    });


    //监听单元格编辑
    table.on('edit(LAY-score-manage)', function (obj) {
        $.ajax({
            url: setter.baseUrl + '/score/modify',
            dataType: "json",
            data: JSON.stringify(obj.data),
            type: "put",
            contentType: 'application/json',
            xhrFields: {
                withCredentials: true
            },
            success: function (res) {
                if (res.code === 200) {
                    layer.msg("修改成功");
                } else {
                    layer.msg("失败");
                }
            },
            error: function (e) {
                layer.msg("失败");
            }
        });
    });

    exports('score', {})
});
